package com.baizhi.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.dao.CmfzArticleDao;
import com.baizhi.dao.CmfzGuruDao;
import com.baizhi.entity.CmfzArticle;
import com.baizhi.entity.CmfzGuru;
import com.baizhi.service.CmfzGuruService;
import com.baizhi.util.FileUpload;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/9 0009.
 */
@Service
public class CmfzGuruServiceImpl implements CmfzGuruService {
    @Autowired
    private CmfzGuruDao cmfzGuruDao;
    @Autowired
    private CmfzArticleDao cmfzArticleDao;
    private org.slf4j.Logger logger = LoggerFactory.getLogger(CmfzGuruServiceImpl.class);

    @Override
    public Map selectAll() {
        Map map = new HashMap();
        try {
            List<CmfzGuru> cmfzGurus = cmfzGuruDao.selectList(null);
            map.put("flag", true);
            map.put("cmfzGurus", cmfzGurus);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag", false);
        }
        return map;
    }

    @Override
    public Map showAll(Integer page, Integer rows, String guruNickname) {
        Map map = new HashMap();

        IPage<CmfzGuru> iPage = new Page<>(page, rows);
        QueryWrapper<CmfzGuru> wrapper = new QueryWrapper<>();
        if (guruNickname != null && !"".equals(guruNickname)) {
            wrapper.like("guru_nickname", guruNickname);
        } else {
            wrapper = null;
        }
        IPage<CmfzGuru> cmfzAlbumIPage = cmfzGuruDao.selectPage(iPage, wrapper);
        List<CmfzGuru> records = cmfzAlbumIPage.getRecords();
        long total = cmfzAlbumIPage.getTotal();

        map.put("total", total);
        map.put("rows", records);

        return map;
    }

    @Override
    public Map add(MultipartFile picture, CmfzGuru cmfzGuru, HttpServletRequest request) {
        Map map = new HashMap();
        try {
            FileUpload.uploadGuguPicture(picture, cmfzGuru, request);
            cmfzGuruDao.add(cmfzGuru);
            map.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag", false);
        }
        return map;
    }

    @Override
    public void export(HttpServletResponse response) {

        /*1.准备写出的数据*/
        List<CmfzGuru> cmfzGurus = cmfzGuruDao.selectList(null);

         /*2.获取Excel文件对象*/

        // 准备参数对象  参数： 1.表头 2.标题 3.类型
        ExportParams params = new ExportParams("上师全部数据", "上师表");

        //参数 1. 导出参数对象 2.被导出的实体类类对象 3.要写出的数据
        Workbook workbook = ExcelExportUtil.exportExcel(params, CmfzGuru.class, cmfzGurus);

        /*3.将Excel文件以附件的方式相应给浏览器*/
        String fileName = "上师表.xls";

        /*获取相应输出流对象*/

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream;charset=UTF-8");
        try {
            // 为了解决中文名称乱码问题
            fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

        try {
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public Map importGuruExcel(MultipartFile excelfile) {
        logger.debug("是否为空{}", excelfile.isEmpty());
        InputStream inputStream = null;
        try {
            inputStream = excelfile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }


        Map map = new HashMap();

        try {
        /*创建导入参数对象*/
            ImportParams importParams = new ImportParams();

            // 设置标题占行数
            importParams.setTitleRows(1);

            // 设置表头占的行数
            importParams.setHeadRows(1);

            //参数 1.读取文件 2.对应的实体类 3.导入参数对象
            List<CmfzGuru> list = ExcelImportUtil.importExcel(inputStream, CmfzGuru.class, importParams);
            logger.debug("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!读取到的内容{}", list);
            for (CmfzGuru cmfzGuru : list) {
                cmfzGuruDao.insert(cmfzGuru);
            }
            map.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag", false);
        }
        return map;
    }

    @Override
    public Map frozen(Integer guruId) {
        Map map = new HashMap();

        try {
            map.put("flag", true);
            cmfzArticleDao.frozenByGuruId(guruId);
            cmfzGuruDao.updateFrozenByGuruId(guruId);
        } catch (Exception e) {
            map.put("flag", false);
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map unFrozen(Integer guruId) {
        Map map = new HashMap();

        try {
            map.put("flag", true);
            cmfzArticleDao.unFrozenByGuruId(guruId);
            cmfzGuruDao.updateUnFroenByGuruId(guruId);
        } catch (Exception e) {
            map.put("flag", false);
            e.printStackTrace();
        }
        return map;
    }
}
